local BC
if select(4,GetBuildInfo()) < 30200 then
	BC = {
		['术士'] = 'WARLOCK',
		['战士'] = 'WARRIOR',
		['猎人'] = 'HUNTER',
		['法师'] = 'MAGE',
		['牧师'] = 'PRIEST',
		['德鲁伊'] = 'DRUID',
		['圣骑士'] = 'PALADIN',
		['萨满祭司'] = 'SHAMAN',
		['潜行者'] = 'ROGUE',
		['死亡骑士'] = 'DEATHKNIGHT',
	}
else
	BC = {}
	for k, v in pairs(LOCALIZED_CLASS_NAMES_MALE) do
		BC[v] = k
	end
	for k, v in pairs(LOCALIZED_CLASS_NAMES_FEMALE) do
		BC[v] = k
	end
end

local function Hex(r, g, b)
	if(type(r) == 'table') then
		if(r.r) then r, g, b = r.r, r.g, r.b else r, g, b = unpack(r) end
	end
	
	if(not r or not g or not b) then
		r, g, b = 1, 1, 1
	end
	
	return format('|cff%02x%02x%02x', r*255, g*255, b*255)
end

local getDiffColor = GetQuestDifficultyColor or GetDifficultyColor
local diffColor = setmetatable({}, {
	__index = function(t,i)
		if not i then return '' end
		local c = getDiffColor(i)
		if not c then return '|cffffffff' end
		t[i] = Hex(c)
		return t[i]
	end
})

local classColorHex = setmetatable({}, {
	__index = function(t,i)
		if not i then return '' end
		local CUSTOM_CLASS_COLORS = CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS
		local eclass = BC[i] or i
		local c = CUSTOM_CLASS_COLORS[eclass]
		if not c then return '|cffffffff' end
		t[i] = Hex(c)
		return t[i]
	end
})

local classColors = setmetatable({}, {
	__index = function(t,i)
		if not i then return '' end
		local CUSTOM_CLASS_COLORS = CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS
		local eclass = BC[i] or i
		local c = CUSTOM_CLASS_COLORS[eclass]
		if not c then return {1,1,1} end
		t[i] = {c.r, c.g, c.b}
		return t[i]
	end
})

if CUSTOM_CLASS_COLORS then
	CUSTOM_CLASS_COLORS:RegisterCallback(function()
		wipe(classColorHex)
		wipe(classColors)
	end)
end

--FRIENDS_LEVEL_TEMPLATE = 'Level %d %s' -- For 'name location' in friends list
local FRIENDS_LEVEL_TEMPLATE = gsub(FRIENDS_LEVEL_TEMPLATE, '%%d', '%%s')
hooksecurefunc('FriendsList_Update', function()
	local friendOffset = FauxScrollFrame_GetOffset(FriendsFrameFriendsScrollFrame)
	local friendIndex
	local playerArea = GetRealZoneText()
	
	for i=1, FRIENDS_TO_DISPLAY, 1 do
		friendIndex = friendOffset + i
		local name, level, class, area, connected, status, note, RAF = GetFriendInfo(friendIndex)
		local nameText = _G['FriendsFrameFriendButton'..i..'ButtonTextName']
		local LocationText = _G['FriendsFrameFriendButton'..i..'ButtonTextLocation']
		local infoText = _G['FriendsFrameFriendButton'..i..'ButtonTextInfo']
		if not name then return end
		if connected then
			nameText:SetVertexColor(unpack(classColors[class]))
			if area == playerArea then
				area = format('|cff00ff00%s|r', area)
				LocationText:SetFormattedText(FRIENDS_LIST_TEMPLATE, area, status)
			end
			level = diffColor[level] .. level .. '|r'
			--class = classColorHex[class] .. class
			infoText:SetFormattedText(FRIENDS_LEVEL_TEMPLATE, level, class)
		else
			return
		end
	end
end)


hooksecurefunc('GuildStatus_Update', function()
	local playerArea = GetRealZoneText()
	
	if ( FriendsFrame.playerStatusFrame ) then
		local guildOffset = FauxScrollFrame_GetOffset(GuildListScrollFrame)
		local guildIndex
		
		for i=1, GUILDMEMBERS_TO_DISPLAY, 1 do
			guildIndex = guildOffset + i
			local name, rank, rankIndex, level, class, zone, note, officernote, online, status, classFileName = GetGuildRosterInfo(guildIndex)
			if not name then return end
			if online then
				local nameText = _G['GuildFrameButton'..i..'Name']
				local zoneText = _G['GuildFrameButton'..i..'Zone']
				local levelText = _G['GuildFrameButton'..i..'Level']
				local classText = _G['GuildFrameButton'..i..'Class']
				
				nameText:SetVertexColor(unpack(classColors[class]))
				if playerArea == zone then
					zoneText:SetFormattedText('|cff00ff00%s|r', zone)
				end
				levelText:SetText(diffColor[level] .. level)
			end
		end
	else
		local guildOffset = FauxScrollFrame_GetOffset(GuildListScrollFrame);
		local guildIndex;
		
		for i=1, GUILDMEMBERS_TO_DISPLAY, 1 do
			guildIndex = guildOffset + i;
			local name, rank, rankIndex, level, class, zone, note, officernote, online, status, classFileName = GetGuildRosterInfo(guildIndex);
			if not name then return end
			if online then
				local nameText = _G['GuildFrameGuildStatusButton'..i..'Name']
				nameText:SetVertexColor(unpack(classColors[class]))
			end
		end
	end
end)


hooksecurefunc('WhoList_Update', function()
	local whoIndex
	local whoOffset = FauxScrollFrame_GetOffset(WhoListScrollFrame);
	
	local playerZone = GetRealZoneText()
	local playerGuild = GetGuildInfo'player'
	local playerRace = UnitRace'player'
	
	for i=1, WHOS_TO_DISPLAY, 1 do
		whoIndex = whoOffset + i;
		local nameText = _G['WhoFrameButton'..i..'Name'];
		local levelText = _G['WhoFrameButton'..i..'Level'];
		local classText = _G['WhoFrameButton'..i..'Class'];
		local variableText = _G['WhoFrameButton'..i..'Variable'];
		
		local name, guild, level, race, class, zone, classFileName = GetWhoInfo(whoIndex);
		if not name then return end
		if zone == playerZone then
			zone = '|cff00ff00' .. zone
		end
		if guild == playerGuild then
			guild = '|cff00ff00' .. guild
		end
		if race == playerRace then
			race = '|cff00ff00' .. race
		end
		local columnTable = { zone, guild, race };
		
		nameText:SetVertexColor(unpack(classColors[class]))
		levelText:SetText(diffColor[level] .. level)
		variableText:SetText(columnTable[UIDropDownMenu_GetSelectedID(WhoFrameDropDown)]);
	end
end)


hooksecurefunc('LFMFrame_Update', function()
	local scrollOffset = FauxScrollFrame_GetOffset(LFMListScrollFrame);
	local resultIndex
	local numResults = GetNumLFGResultsProxy();
	
	for i=1, LFGS_TO_DISPLAY, 1 do
		resultIndex = scrollOffset + i;
		if resultIndex > numResults then return end
		
		local name, level, zone, class, criteria1, criteria2, criteria3, comment, numPartyMembers, isLFM, classFileName, willBeLeader, willBeTank, willBeHealer, willBeDPS = GetLFGResultsProxy(resultIndex);
		if not name then return end
		local nameText = _G['LFMFrameButton'..i..'Name'];
		local levelText = _G['LFMFrameButton'..i..'Level'];
		local classText = _G['LFMFrameButton'..i..'Class'];
		
		nameText:SetVertexColor(unpack(classColors[class]))
		levelText:SetText(diffColor[level] .. level)
	end
end)

hooksecurefunc('WorldStateScoreFrame_Update', function()
	local inArena = IsActiveBattlefieldArena()
	for i = 1, MAX_WORLDSTATE_SCORE_BUTTONS do
		local index = FauxScrollFrame_GetOffset(WorldStateScoreScrollFrame) + i
		local name, killingBlows, honorableKills, deaths, honorGained, faction, rank, race, class, classToken, damageDone, healingDone = GetBattlefieldScore(index)
		if name then
			local _, _, playerName, playerServer = name:find('([^%-]+)%-(.+)')
			local text = classColorHex[classToken] .. (playerName or name) .. '|r'
			if playerServer then
				if inArena then
					text = text .. (faction==0 and '-|cff19ff19' or '-|cffffd100') .. playerServer
				else
					text = text .. (faction==0 and '-|cffff1919' or '-|cff00adf0') .. playerServer
				end
			end
			
			local buttonNameText = _G['WorldStateScoreButton' .. i .. 'NameText']
			buttonNameText:SetText(text)
		end
	end
end)
